/**
 * 文件上传
 */
myApp.controller('FileUploadCtrl', function ($scope, $uibModal, $uibModalInstance, info, $timeout) {
    $scope.info = info;
    $scope.info.dir = '';

    $scope.info.type = '';
    angular.forEach($scope.info.allowedFileExtensions, function (t) {
        $scope.info.type += t + ','
    });

    //初始化文件上传控件
    $scope.initFileInput = function () {
        $("#uploadFile").fileinput({
            language: 'zh', //设置语言
            uploadUrl: $scope.info.uploadUrl,
            showUpload: true,
            uploadAsync: true,
            allowedFileExtensions: $scope.info.allowedFileExtensions,//接收的文件后缀
            msgInvalidFileExtension: '文件类型错误,只支持' + $scope.info.type.substring(0, $scope.info.type.length - 1) + '类型文件',
            uploadExtraData: function () {
                $scope.info.data.token_id = getTokenId();
                if ($scope.info.dir != '') {
                    $scope.info.data.dir = $scope.info.dir;
                }
                return $scope.info.data;
            }
        }).on("fileuploaded", function (event, data, previewId, index) {
            console.log(JSON.stringify(data.response));
            if (!data.response.success) {
                alert(data.response.msg);
                return;
            }
            ;
            $uibModalInstance.dismiss('save');
        });
    };

    $timeout(function () {
        $scope.initFileInput();
    }, 500);

    $scope.cancel = function () {
        // 关闭弹出框
        $uibModalInstance.close('cancel');
    };
});